<template>
  <div class="app-container">
    <div class="search-container">
      <el-form ref="form" :model="queryParams" :inline="true">
        <el-form-item prop="name" label="关联物名称">
          <el-input
            v-model="queryParams.name"
            placeholder="关联物名称"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item prop="type" label="关联类型">
          <el-select v-model="queryParams.type" placeholder="关联类型">
            <el-option label="基因" value="GENE"></el-option>
            <el-option label="饮食" value="DIET"></el-option>
            <el-option label="代谢物" value="METABOLIN"></el-option>
            <el-option label="表型" value="PHENOTYPE"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery" icon="el-icon-search">搜索</el-button>
          <el-button @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never">
      <template #header>
        <el-button type="success" @click="openDialog()"
          ><i class="el-icon-circle-plus"></i> 新增</el-button>
        <el-button
          type="danger"
          :disabled="ids.length === 0"
          @click="handleDelete()"
          ><i class="el-icon-delete"></i> 删除</el-button>
      </template>

      <el-table
        v-loading="loading"
        :data="dataList"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="序号" width="55" prop="dataIndex"/>
        <el-table-column label="微生物关联物名称" prop="name"/>
        <el-table-column label="关联类型" prop="typeText"/>
        <el-table-column label="创建时间" prop="createTimeText"/>
        <el-table-column label="更新时间" prop="updateTimeText"/>
        <el-table-column fixed="right" label="操作"  min-width="280" width="280">
          <template #default="scope">
            <el-button
              type="warning"
              size="small"
              link
              @click="openDialog(scope.row.id, scope.row.type)"
            ><i class="el-icon-edit"></i> 编辑</el-button>
            <el-button
              type="danger"
              size="small"
              link
              @click="handleDelete(scope.row.id, scope.row.type)"
            ><i class="el-icon-delete"></i> 删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryParams.pageNum"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </el-card>

    <!-- 微生物关联物表单弹窗 -->
    <el-dialog
      :visible.sync="dialog.visible"
      :title="dialog.title"
      width="500px"
      :before-close="closeDialog"
    >
      <el-form
        ref="userFormRef"
        :model="formData"
        :rules="rules"
        label-width="150px"
      >
        <el-form-item label="微生物关联物名称" prop="name">
          <el-input v-model="formData.name" placeholder="请输入微生物关联物名称" />
        </el-form-item>
        <el-form-item label="关联类型" prop="type">
          <el-select v-model="formData.type" placeholder="请输入关联类型">
            <el-option label="基因" value="GENE"></el-option>
            <el-option label="饮食" value="DIET"></el-option>
            <el-option label="代谢物" value="METABOLIN"></el-option>
            <el-option label="表型" value="PHENOTYPE"></el-option>
          </el-select>
        </el-form-item>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="addOrUpdate">确 定</el-button>
          <el-button @click="closeDialog">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>

import organismCorrelationApi from '@/api/knowledge/organismCorrelation'
import { formatDate } from '@/utils/formatTime'
export default {
	name: 'dictIndex',
	data() {
		return {
			queryParams: {
        name: '',
        type: '',
        pageNum: 0,
        pageSize: 10,

      },
      total: 0,
      loading: false,
      dataList: [],
      ids: [],
      dialog: {},
      formData: {},
      rules: {},

      typeMap: {
        PHENOTYPE: '表型',
        GENE: '基因',
        METABOLIN: '代谢物',
        DIET: '饮食',
      },
		};
	},
  methods: {
    handleQuery() {
      this.loading = true;
      let params = {
        number: this.queryParams.pageNum,
        size: this.queryParams.pageSize,
        name: this.queryParams.name,
        type: this.queryParams.type,
      };
      organismCorrelationApi.page(params).then((res) => {
        let data = res.data;
        let that = this;
        let index = 1
        data.content.forEach((v) => {
          v.createTimeText = formatDate(new Date(v.createTime), "YYYY-mm-dd HH:MM:SS")
          v.updateTimeText = formatDate(new Date(v.updateTime), "YYYY-mm-dd HH:MM:SS")
          v.typeText = that.typeMap[v.type]
          v.dataIndex = index++;
        });
        this.dataList = data.content;
        
        this.total = data.count;
        this.loading = false;
      }).catch(() => {});
    },
    resetQuery() {
      this.queryParams = {
        keywords: '',
        pageNum: 0,
        pageSize: 10,
      };
      this.handleQuery();
    },
    
    addOrUpdate() {
      let that = this;
      if (this.formData.id) {
        organismCorrelationApi.update(this.formData).then((res) => {
          this.$message({message: '微生物关联物信息更新成功！', type: 'success'});
          that.closeDialog();
        });
      } else {
        organismCorrelationApi.add(this.formData).then((res) => {
          this.$message({message: '微生物关联物信息添加成功！', type: 'success'});
          that.closeDialog();
        });
      }
    },
    openDialog(id, type) {
      this.$set(this.dialog, 'visible', true);
      let that = this;
      if (id) {
        this.$set(this.dialog, 'title', '编辑微生物关联物信息');
        organismCorrelationApi.get(id, type).then((res) => {
          let data = res.data;
          that.formData = data;

        });
      } else {
        this.$set(this.dialog, 'title', '新增微生物关联物信息');
        that.formData = {};
      }

    },
    closeDialog() {
      this.$set(this.dialog, 'visible', false);
      this.$set(this.dialog, 'title', '');
      this.handleQuery();
    },
    handleDelete(id, type) {
      if (id) {
        organismCorrelationApi.delete(id, type).then((res) => {
          this.$message({message: '微生物关联物信息删除成功！', type: 'success'});
          this.handleQuery();
        });
      } else {
        this.$message.error('暂不支持批量删除微生物关联物信息！');
      }
    },
    handleSelectionChange(val) {
      this.ids = [];
      val.forEach(v => {
        this.ids.push(v.id);
      });
    },
    handleSelectionRoleChange(rows) {
      if (rows) {
        let arr = [];
        rows.forEach((v) => {
          arr.push(v.id);
        });
        this.selectRoleIdArr = arr;
      }
    },
    handleSizeChange(val) {
      this.queryParams.pageSize = val;
      this.handleQuery();
    },
    handleCurrentChange(val) {
      this.queryParams.pageNum = val;
      this.handleQuery();
    },
  },
  created() {
    this.handleQuery();
  }
};
</script>

<style scoped>
.el-pagination {
  margin-top: 10px;
  float: right;
  margin-bottom: 10px;
}
</style>